package org.shuma.goods.service.impl;


import org.shuma.goods.domain.entity.ProductBasicInfo;
import org.shuma.goods.domain.entity.ProductDetailsInfo;
import org.shuma.goods.mapper.ProductBasicInfoMapper;
import org.shuma.goods.mapper.ProductDetailsInfoMapper;
import org.shuma.goods.service.ProductDetailsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (ProductDetailsInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-06-18 13:50:09
 */
@Service("productDetailsInfoService")
public class ProductDetailsInfoServiceImpl implements ProductDetailsInfoService {
    @Autowired
    private ProductDetailsInfoMapper productDetailsInfoMapper;
    @Autowired
    private ProductBasicInfoMapper productBasicInfoMapper;
//    @Resource
//    private ProductDetailsInfoMapper productDetailsInfoMapper;

    @Override
    public ProductDetailsInfo getProductDetailsInfo(Integer productId) {
        ProductBasicInfo productBasicInfo = productBasicInfoMapper.selectById(productId);

        return productDetailsInfoMapper.selectById(productBasicInfo.getPDetailsId());

    }

    /**
     * 通过ID查询单条数据
     *
     * @param pDetailsId 主键
     * @return 实例对象
     */
    @Override
    public ProductDetailsInfo queryById(Long pDetailsId) {
        return this.productDetailsInfoMapper.queryById(pDetailsId);
    }

    /**
     * 分页查询
     *
     * @param productDetailsInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ProductDetailsInfo> queryByPage(ProductDetailsInfo productDetailsInfo, PageRequest pageRequest) {
        long total = this.productDetailsInfoMapper.count(productDetailsInfo);
        return new PageImpl<>(this.productDetailsInfoMapper.queryAllByLimit(productDetailsInfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param productDetailsInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ProductDetailsInfo insert(ProductDetailsInfo productDetailsInfo) {
        this.productDetailsInfoMapper.insert(productDetailsInfo);
        return productDetailsInfo;
    }

    /**
     * 修改数据
     *
     * @param productDetailsInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ProductDetailsInfo update(ProductDetailsInfo productDetailsInfo) {
        ProductDetailsInfo productDetailsInfo1 = productDetailsInfo;
        productDetailsInfo1.setPDetailsId(null);
        if (productDetailsInfo.getPDetailsId() == null || productDetailsInfo1==null){
            throw new RuntimeException();
        }
        this.productDetailsInfoMapper.update(productDetailsInfo);
        return this.queryById(productDetailsInfo.getPDetailsId());
    }

    /**
     * 通过主键删除数据
     *
     * @param pDetailsId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long pDetailsId) {
        return this.productDetailsInfoMapper.deleteById(pDetailsId) > 0;
    }
}
